/*
 * Copyright 2011 derek.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.aocore.map;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JFrame;

/**
 *
 * @author derek
 */
public class MapFrame extends JFrame
{
    static Color colors[] = {Color.BLACK, Color.BLUE, Color.RED, Color.GREEN, Color.YELLOW, Color.LIGHT_GRAY, Color.CYAN, Color.MAGENTA, Color.WHITE, Color.PINK, Color.ORANGE, Color.DARK_GRAY, Color.LIGHT_GRAY, Color.LIGHT_GRAY, Color.LIGHT_GRAY, Color.LIGHT_GRAY, Color.LIGHT_GRAY, Color.LIGHT_GRAY};
    private Map map;
    private BufferedImage image;
    private List<Integer> list = new LinkedList<Integer>();

    public void setMap(Map map)
    {
        this.map = map;
        this.image = new BufferedImage(map.getWidth(), map.getHeight(), BufferedImage.TYPE_INT_RGB);

        for (int x = 0; x < map.getWidth(); x++)
        {
            for (int y = 0; y < map.getHeight(); y++)
            {
                int i = map.get(x, y);

                if (list.indexOf(i) == -1)
                {
                    list.add(i);
                }

                image.setRGB(x, y, colors[list.indexOf(i)].getRGB());
            }
        }

        this.setSize(map.getWidth(), map.getHeight());
        this.setResizable(false);
        this.setVisible(true);
        this.repaint();
    }

    @Override
    public void paint(Graphics g)
    {
        if (map != null)
        {
            g.drawImage(image, 0, 0, this);
        }
    }
}
